﻿using System.Linq;
using Microsoft.Office.Interop.Excel;

namespace ExcelAutomator.Validation
{
	public class ExcelValidator : BaseValidator
	{
		private readonly Workbook m_Workbook;

		public ExcelValidator(Workbook workbook)
		{
			m_Workbook = workbook;
		}

		public ExcelValidator WorksheetExists(string worksheetName)
		{
			#region pre

			if (string.IsNullOrEmpty(worksheetName))
			{
				return this;
			}

			#endregion


			if (worksheetName.Contains('!'))
			{
				worksheetName = worksheetName.Split('!')[0];
			}


			Worksheet worksheet;
			if (!m_Workbook.TryGetWorksheet(worksheetName, out worksheet))
			{
				SetFailure(string.Format("The worksheet '{0}' doesn't exists.", worksheetName), "WorksheetExists");
			}
			return this;
		}
		
	}
}
